package com.bjdd.jdy.ui.register;

import com.bjdd.jdy.Constant;
import com.bjdd.jdy.entity.TokenResult;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SignUtils;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StringUtils;
import com.bjdd.jdy.util.T;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shq1 on 2017/4/2.
 */

public class RegisterPresenter extends RegisterContract.Presenter {
    @Override
    public void register(String code, String phone, String pwd) {
        String timestamp = SignUtils.getTime();
        Map<String,String> map = new HashMap<>();
        map.put("mobile",phone);
        pwd = StringUtils.MD5(pwd);
        map.put("pwd",pwd);
        map.put("code",code);
        map.put("timestamp",timestamp);
        String sign = SignUtils.getSignature(map);
        showLoadingDialog(false);
        rxManager.add(model.getRegisterModel(phone,pwd,code,timestamp,sign)
                    .subscribe(result -> {

                        if (!result.isError()){
                            SpUtil.setToken(result.getData().getToken());
                            getUserInfo(result.getData());
                        }else {
                            hideLoadingDialog();
                            T.show(result.getMsg());
                        }
                    },e -> {
                        hideLoadingDialog();
                        T.show("注册失败");
                        e.printStackTrace();
                    }));
    }

    @Override
    public void getCode(String phone) {
        L.d("getCode:"+phone);
        String timestamp = SignUtils.getTime();
        Map<String,String> map = new HashMap<>();
        map.put("mobile",phone);
        map.put("timestamp",timestamp);
        String sign = SignUtils.getSignature(map);
        showLoadingDialog(false);
        rxManager.add(model.getCodeModel(phone,timestamp,sign)
                .subscribe(result -> {
                    hideLoadingDialog();
                    T.show(result.getMsg());
                },e-> {
                    hideLoadingDialog();
                    T.show("获取验证码失败");
                    e.printStackTrace();
                })
        );
    }

    @Override
    public void forgetCommit(String code, String phone, String pwd) {
        String timestamp = SignUtils.getTime();
        Map<String,String> map = new HashMap<>();
        map.put("mobile",phone);
        map.put("code",code);
        pwd = StringUtils.MD5(pwd);
        map.put("pwd",pwd);
        map.put("timestamp",timestamp);
        String sign = SignUtils.getSignature(map);
        showLoadingDialog(false);
        rxManager.add(model.getResultModel(phone,pwd,code,timestamp,sign)
                .subscribe(result -> {
                    hideLoadingDialog();
                    if (!result.isError()){
                        view.resetSuccess();
                    }else {
                        T.show(result.getData());
                    }
                },e -> {
                    hideLoadingDialog();
                    T.show("修改失败");
                }));
    }

    @Override
    public void getForgetCode(String phone) {
        String timestamp = SignUtils.getTime();
        Map<String,String> map = new HashMap<>();
        map.put("mobile",phone);
        map.put("timestamp",timestamp);
        String sign = SignUtils.getSignature(map);
        showLoadingDialog(false);
        rxManager.add(model.getForgetCodeModel(phone,timestamp,sign)
                .subscribe(result -> {
                    hideLoadingDialog();
                    T.show(result.getMsg());
                },e -> {
                    hideLoadingDialog();
                }));
    }

    private void getUserInfo(TokenResult data){
        rxManager.add(model.getUserInfo(data.getToken(),data.getUserId())
                .subscribe(result -> {
                    hideLoadingDialog();
                    if (!result.isError()){
                        SpUtil.setUser(result.getData());
                        view.registerSuccess(result.getData());
                    }else {
                        T.show(result.getMsg());
                    }
                },e -> {
                    hideLoadingDialog();
                }));
    }

    @Override
    public void onStart() {

    }
}
